﻿$(function() {
    
    
    // Cuffons.
    
    Cufon.replace('#header #paybox #cart .title, \
        #navigation,\
        #newsletterbox fieldset p,\
    ', { fontFamily: 'Proxima Nova Rg' });
    
    window.setTimeout(function() {
        Cufon.replace('#header #paybox #cart .title', { fontFamily: 'Proxima Nova Rg' });
    },1000);
    

    // Eventos
    $('#newsletterbox').mousemove(function() {
        Cufon.replace('#newsletterbox fieldset p', { fontFamily: 'Proxima Nova Rg' });
    });

    
    
    $('.products ul').each(function() {
    
        $(this).jQRoll();
    
    });
    
    
    //Prateleiras Compostas
    $('.prateleira ul li:not(li li)').each(function(){
        
        var group = $(this).find('.imagesprateleiras .group');
        var box = $(this).find('.box');

        if ( $(group).size() > 0 ){
            $(box).empty();
            $(box).html( $(group).eq(0).html() );
        }
        
        $('.coresprateleiras a:first').addClass('active');
    
    });
    
    $('li .coresprateleiras a').click(function(){
        
        var index = $(this).index('li .coresprateleiras a');
        var onwer = $(this).parent().parent();
        var box = $(onwer).find('.box');
        var group = $(onwer).find('.imagesprateleiras .group');

        $(box).empty().html( $(group).eq(index).html() );
        
        $('li .coresprateleiras a').removeClass('active');
        $(this).addClass('active');
        
    });
    
    
    //Mouse Prateleiras
    prateleiraContinua = null
    prateleiraLargura = 185;
    step1 = ( prateleiraLargura / 3 );
    step2 = ( prateleiraLargura / 3 ) * 2;

    $(".prateleira ul li .box").mousemove(function(e){
        
        var offset = $(this).offset();
        
        if ( e.pageX < offset.left + step1 ){
            $(this).find('span').hide();
            $(this).find('.image1').css({ 'display' : 'block' });
        }else if (  e.pageX < offset.left + step2 ){
            $(this).find('span').hide();
            $(this).find('.image2').css({ 'display' : 'block' });
        }else if (  e.pageX < offset.left + prateleiraLargura ){
            $(this).find('span').hide();
            $(this).find('.image3').css({ 'display' : 'block' });
        }
        
    });
   
        
        
});

function updatePrateleiras (owner){

    

}


// Begin the plugin resposable of move the product list when the limit has passed.
$.fn.jQRoll = (function (options) {
    // Defining all customs variables.
    var options = $.extend({
        selectInstance : $(this),
        selectList : $(this).find("li"),
        selectListSize : $(this).find("li").size(),
        containerInstance : null,
        containerLimit : 3,
        moveSize : $(this).find('li:first-child').width() + 20,
        prevButton :  null,
        nextButton :  null,
        track : null
    },options);
    options.track = 0;
    var boxindex = 0;
    
    // appending a container to the mathed ul.
    $(options.selectInstance).before('<div id="JQRollInstance"> </div>');
    // Matching the added container to a variable.
    options.containerInstance = $(options.selectInstance).prev();
    // Appending the mathed ul on the container.
    $(options.containerInstance).append(options.selectInstance);
    // Appeding the previously button.
    $(options.containerInstance).before('<a class="JQPrevButton disabled" href="javascript:void(0)">Prev</a>');
    // Appeding the next button
    $(options.containerInstance).after('<a class="JQNextButton" href="javascript:void(0)">Next</a>');
    // Finding the prev button.
    options.prevButton = $(options.containerInstance).parent().find(".JQPrevButton");
    // Finding the next button.
    options.nextButton = $(options.containerInstance).parent().find(".JQNextButton");
    
    if ( options.selectListSize <= options.containerLimit )
    {
    
        $(options.nextButton).addClass("disabled");
    
    }

    
    // When the prev buttton has clicked.
    $(options.nextButton).click(function () {
        

    
        // if the track value > 0 then can move
        //if( options.track >= options.moveSize || options.track - options.moveSize  ) {
        if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
        
            options.track -= options.moveSize;
            boxindex += 1;
            
            $(options.selectInstance).animate({ left : options.track }, 500);
            
            if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
            
                $(options.nextButton).removeClass('disabled');
                
            }else{
                
                $(options.nextButton).addClass('disabled');
            
            }
            
            if( options.track < 0 ) {
            
                $(options.prevButton).removeClass('disabled');
                
            }else{
                
                $(options.prevButton).addClass('disabled');
            
            }
            
        }
        
    });
    
    // When the next button has clicked.
    $(options.prevButton).click(function () {
    
        //if( options.track < ( options.moveSize * options.selectListSize )  && options.track < ( options.moveSize * options.containerLimit )  ) {
        if( options.track < 0 ) {
            
            options.track += options.moveSize;
            boxindex -= 1;
            
            $(options.selectInstance).animate({ left : options.track }, 500);

            if( options.track < 0 ) {
            
                $(this).removeClass('disabled');
                
            }else{
                
                $(this).addClass('disabled');
            
            }
            
            if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
            
                $(options.nextButton).removeClass('disabled');
                
            }else{
                
                $(options.nextButton).addClass('disabled');
            
            }
            
        }

    
    });
    
});